package problem72;

//72.编辑距离
//https://leetcode.cn/problems/edit-distance/?envType=study-plan-v2&envId=top-100-liked

class Solution {
    public int minDistance(String s1, String s2) {
        s1 = " " + s1;
        s2 = " " + s2;
        int n = s1.length(), m = s2.length();
        int[][] dp = new int[n][m];
        for(int i = 1; i<n; i++) {
            dp[i][0] = i;
        }
        for(int j = 1; j<m; j++) {
            dp[0][j] = j;
        }
        for(int i = 1; i<n; i++) {
            for(int j = 1; j<m; j++) {
                if(s1.charAt(i) == s2.charAt(j)) {
                    dp[i][j] = dp[i-1][j-1];
                }else {
                    //替换, 删除, 插入
                    dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i-1][j], dp[i][j-1])) + 1;
                }
            }
        }
        return dp[n-1][m-1];
    }
}

/**

dp[i][j]: 将s1的前i个字符转化为s2的前j个字符所使用的最少操作数

 */